﻿Public Class Form
    Dim departments
    Private Const code = "SKPD003"

    Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
        Dim skpd = cbDepartment.SelectedItem.ToString()
        Dim i As String = ""
        For Each d As DepartmentSvc.department In departments
            If d.nama.Equals(skpd) Then
                i = d.id
            End If
        Next

        Dim pegawaiSvc = New PegawaiSvc.PegawaiService()
        Dim pegawais = pegawaiSvc.getByDepartment(i)

        Dim list = generateData(pegawais)
        isiDataGrid(list)
    End Sub

    Private Sub btnKeluar_Click(sender As Object, e As EventArgs) Handles btnKeluar.Click
        Close()
    End Sub

    Private Sub FormLoaded(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim deptSvc = New DepartmentSvc.DepartmentService()
        departments = deptSvc.getAll()
        cbDepartment.Items.Add("")
        For Each department As DepartmentSvc.department In departments
            cbDepartment.Items.Add(department.nama)
        Next

        Dim bulan As Array
        bulan = System.Enum.GetNames(GetType(CalendarUtil.Bulan))
        cbBulan.Items.Add("")
        For Each Str As String In bulan
            cbBulan.Items.Add(Str)
        Next
    End Sub

    Private Function generateData(pegawais() As PegawaiSvc.pegawai) As List(Of Gaji)
        Dim list = New List(Of Gaji)
        Dim bulan As Integer
        Dim tahun As Integer

        Try
            bulan = CalendarUtil.getBulanNumber(cbBulan.SelectedItem.ToString())
            tahun = Integer.Parse(cbTahun.SelectedItem.ToString())
        Catch E As NullReferenceException
            bulan = Date.Today.Month
            tahun = Date.Today.Year
        End Try

        For Each pegawai As PegawaiSvc.pegawai In pegawais
            Dim lemburs = New LemburSvc.LemburService().getByPegawaiBulanan(pegawai.nip, bulan, tahun)
            Dim presensis = New PresensiSvc.PresensiService().getByPegawaiBulanan(pegawai.nip, bulan, tahun)

            Dim totalLembur = hitungTotalLembur(lemburs)
            Dim totalPresensi = presensis.Count
            Dim gaji = New GajiFactory().CreateGaji(pegawai.nip, pegawai.nama, pegawai.golongan, totalPresensi, totalLembur)
            list.Add(gaji)
        Next

        Return list
    End Function

    Private Function hitungTotalLembur(lemburs() As LemburSvc.lembur) As Integer
        Dim totalLembur = 0
        For Each lembur As LemburSvc.lembur In lemburs
            totalLembur += lembur.jumlah
        Next

        Return totalLembur
    End Function

    Private Sub isiDataGrid(list As List(Of Gaji))
        dgData.DataSource = list
    End Sub

    Private Sub dgData_DoubleClick(sender As Object, e As EventArgs) Handles dgData.DoubleClick
        Try
            Dim rowIndex = dgData.CurrentRow.Index

            Dim gaji = New Gaji
            gaji.Nip = dgData.Rows.Item(rowIndex).Cells.Item(0).Value
            gaji.Nama = dgData.Rows.Item(rowIndex).Cells.Item(1).Value
            gaji.Golongan = dgData.Rows.Item(rowIndex).Cells.Item(2).Value
            gaji.Lembur = dgData.Rows.Item(rowIndex).Cells.Item(3).Value
            gaji.GajiPokok = dgData.Rows.Item(rowIndex).Cells.Item(4).Value
            gaji.GajiLembur = dgData.Rows.Item(rowIndex).Cells.Item(5).Value
            gaji.GajiLemburTotal = dgData.Rows.Item(rowIndex).Cells.Item(6).Value
            gaji.GajiTotal = dgData.Rows.Item(rowIndex).Cells.Item(7).Value
            'gaji.Presensi = dgData.Rows.Item(rowIndex).Cells.Item(3).Value
            'gaji.UangMakan = dgData.Rows.Item(rowIndex).Cells.Item(6).Value

            Dim formGaji = New FormGaji()
            formGaji.SetGaji(gaji)
            formGaji.Show()
        Catch Exc As NullReferenceException
            'do nothing
        End Try
    End Sub
End Class
